package com.rtx.wcs.ess.ristowcs;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rtx.edi.platform.synctask.service.ConvertService;
import com.rtx.wcs.std.bean.po.RtxSowdetail;
import com.rtx.wcs.std.handle.ristowcs.bztask.AbsBZTaskDownHandle;


/**
 * 播种任务
 * @author Administrator
 *
 */
@Service("BZ-TASK")
public class BZTASK extends AbsBZTaskDownHandle{
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Value("${ess.bz.down.url}")
	private String BZ_TASK_URL;
	
	@Autowired
	ConvertService convertService;

	//		 {
	//			  "code": -1,
	//			  "message": "请求失败!",
	//			  "data": {}
	//			}
	@Override
	protected Map<String, String> handleResponse(RtxSowdetail o, String response) {
		Map<String,String> resultMap = new HashMap<>();
		resultMap.put("editflag", "");//PS为成功 PE为失败
		resultMap.put("message", "");
		resultMap.put("data", "");
		resultMap.put("status", "");
		
		try {
			JSONObject res = JSON.parseObject(response);
			if("0".equals(res.getString("code"))) {
				resultMap.put("editflag", "PS");
			}else {
				resultMap.put("editflag", "PE");
			}
			resultMap.put("message", res.getString("msg"));
		} catch (Exception e) {
			logger.error("",e);
			resultMap.put("message", response);
			resultMap.put("editflag", "PE");
		}
		return resultMap;
	}



	//示例报文
	//	{
	//		"controllerCode":"group01", 
	//		"index":"0001", 
	//		"tagCode":"CS-001-001-01", 
	//		"color":"RED", 
	//		"mode":"LIGHTDARKFLASH", 
	//		"display":"777" 
	//		}
	@Override
	protected JSONObject converWcsRequest(RtxSowdetail t,String mappingcode) {
		
		if(t.getConfig() == null) {
			throw new RuntimeException("系统代码[RTXSOWPOSITION]中udf1播种设备["+t.getSowdevice()+"]格口号["+t.getSeqno()+"]未配置");
		}
		
		if(!StringUtils.hasLength(t.getConfig().getCode())) {
			throw new RuntimeException("系统代码[RTXSOWPOSITION]中udf1播种设备["+t.getSowdevice()+"]格口号["+t.getSeqno()+"]未配置code设备格口号");
		}
		JSONObject edirequest = new JSONObject();
		edirequest.put("controllerCode", t.getSowdevice());
		edirequest.put("index", t.getSowdevice());
		edirequest.put("tagCode", t.getConfig().getCode());
		edirequest.put("color", "BLUE");
		edirequest.put("mode", "LIGHT");
		edirequest.put("display", t.getOriginalqty());
		
		return edirequest;
	}


	@Override
	protected String getEdiRequestURL(String mappingcode) {
		return BZ_TASK_URL;
	}

}
